package com.pussinboots.morning.sync.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pussinboots.morning.sync.dao.ProductCategoryMapper;
import com.pussinboots.morning.sync.entity.Product;
import com.pussinboots.morning.sync.entity.ProductCategory;
import com.pussinboots.morning.sync.service.IProductCategoryService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品分类关联表 服务实现类
 * </p>
 *
 * @author BaoZhou
 * @since 2019-09-16
 */
@Service
@Slf4j
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements IProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public boolean saveDefaultProductCategory(List<Product> productList) {
        val productCategories = new ArrayList<ProductCategory>();
        ProductCategory productCategory;
        for (Product product : productList) {
            if (product.getProductId() != null) {
                productCategory = ProductCategory.builder().categoryId(46L).productId(product.getProductId()).build();
                productCategories.add(productCategory);
            }
        }
        this.saveBatch(productCategories);
        return true;
    }

    @Override
    public boolean delProductCategory(List<Long> productIds) {
        return this.remove(new QueryWrapper<ProductCategory>().in("product_id", productIds));
    }

    @Override
    public boolean delCategoryByErpId(List<String> erpIds) {
        try {
            productCategoryMapper.delCategoryByErpId(erpIds);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        return false;
    }
}